﻿using System;
using System.Collections.Generic;
using Sunlight.Dispatch.UI.Properties;
using Sunlight.Dispatch.Model;
using Newtonsoft.Json;
using System.Linq;
using Sunlight.Dispatch.UI.Controls;

namespace Sunlight.Dispatch.UI {
    public partial class GranlaritySelectFrm : BaseFrm {
        public GranlaritySelectFrm() {
            InitializeComponent();
            this.Load += GranlaritySelectFrm_Load;
        }

        private void GranlaritySelectFrm_Load(object sender, EventArgs e) {
            var timeGranularityJson = Settings.Default.TimeGranularity;
            var stimeGranularity = JsonConvert.DeserializeObject<List<KeyValueItem>>(timeGranularityJson);
            this.comboBoxGranlarity.DataSource = stimeGranularity;
            this.comboBoxGranlarity.DisplayMember = "Value";
            this.comboBoxGranlarity.ValueMember = "Key";
            if(stimeGranularity.Any(d => d.IsDefault)) {
                var defaultKeyValue = stimeGranularity.First(d => d.IsDefault);
                this.comboBoxGranlarity.SelectedItem = defaultKeyValue;
            }
            this.TopLevel = true;
        }

        private void btnConfig_Click(object sender, EventArgs e) {
            if(this.comboBoxGranlarity.SelectedItem == null) {
                MessageBoxEx.Show(this, "请选择时间粒度", "提示");
                return;
            }
            this.RasieExtendEvent(this.comboBoxGranlarity.SelectedItem);
        }
    }
}
